// pages/Server/Server.js
const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    img:'',
    TabCur: 0,
    scrollLeft: 0,
    searchList:['全部','考研资料','考试资料','失物招领','校园美景'],
    searckKey:'',
    //数据展示列表
    bbsList:[] 

  },
  getkey(e) {
    this.setData({
      searchKey: e.detail.value
    })
  },
  search() {
    var that = this;
    if(this.data.searchKey == '发布'){
      wx.navigateTo({
        url: 'Pub/Pub',
      })
    }else{
      that.setData({

        bbsList: []
      }, () => {
        that.requestData();
      })
    }
    
  },
  getdetail(e) {
    var that = this;
    wx.setStorage({
      key: 'selbbs',
      data: e.currentTarget.dataset.info,
      success() {
        wx.navigateTo({
          url: 'Detail/Detail?type=' + e.currentTarget.dataset.info.type,
        })
      }
    })
  },
  requestData() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    if (that.data.TabCur == 0 && that.data.searchKey == '') {
      console.log('全局搜索')
      db.collection('bbs').orderBy('dbdate', 'desc').skip(that.data.bbsList.length).limit(10).get({
        success(res) {
          that.setData({
            bbsList: that.data.bbsList.concat(res.data)
          }, () => {
            wx.hideLoading();
            console.log(that.data.bbsList)
          })
        }
      })
    } else if (that.data.searchKey == '') {
      console.log('搜索' + that.data.searchList[that.data.TabCur])
      db.collection('bbs').orderBy('dbdate', 'desc').skip(that.data.bbsList.length).limit(10).where({
        type: that.data.searchList[that.data.TabCur]
      }).get({
        success(res) {
          that.setData({
            bbsList: that.data.bbsList.concat(res.data)
          }, () => {
            wx.hideLoading();
            console.log(that.data.bbsList)
          })
        }
      })
    } else if (that.data.searchList[that.data.TabCur] !='全部'){
      console.log('搜索关键词' + that.data.searchKey)
      db.collection('bbs').orderBy('dbdate', 'desc').skip(that.data.bbsList.length).limit(10).where(_.or([{
        content: db.RegExp({
          regexp: that.data.searchKey,
          options: 'm',
        })
      },
      {
        title: db.RegExp({
          regexp: that.data.searchKey,
          options: 'm',
        })
      }]).and({
        type: that.data.searchList[that.data.TabCur]
      })
      ).get({
        success(res) {
          that.setData({
            bbsList: that.data.bbsList.concat(res.data)
          }, () => {
            wx.hideLoading();
            console.log(that.data.bbsList)
          })
        }
      })
    } else if (that.data.searchList[that.data.TabCur] == '全部'){
      console.log('搜索关键词' + that.data.searchKey)
      db.collection('bbs').orderBy('dbdate', 'desc').skip(that.data.bbsList.length).limit(10).where(_.or([{
        content: db.RegExp({
          regexp: that.data.searchKey,
          options: 'm',
        })
      },
      {
        title: db.RegExp({
          regexp: that.data.searchKey,
          options: 'm',
        })
      }])
      ).get({
        success(res) {
          that.setData({
            bbsList: that.data.bbsList.concat(res.data)
          }, () => {
            wx.hideLoading();
            console.log(that.data.bbsList)
          })
        }
      })
    }
  },

  tabSelect(e) {
    var that = this;
    this.setData({
      searchKey: '',
      bbsList: [],
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    }, () => {
      console.log(this.data.TabCur)
      //如果是发布的话就是进入发布界面
      if (this.data.TabCur == 7 && app.eduOS.xh != '') {
        wx.navigateTo({
          url: 'Pub/Pub',
        })
      } else {
        //根据SearchKey 和 Type 请求数据
        that.requestData();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ img: app.globalData.imgList.funcimg })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if(that.data.bbsList.length == 0){
      that.setData({
        bbsList: [],
        TabCur: 0,
        searchKey: ''
      }, () => {
        that.requestData();
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      searchKey: '',
      bbsList: []
    }, () => {
      wx.stopPullDownRefresh();
      that.requestData();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})